from rest_framework import serializers
from spit.models import Spit, Comments


class SpitSerializer(serializers.ModelSerializer):
    """吐槽首页序列化器"""
    class Meta:
        model = Spit
        fields = ('id', 'abstract', 'create_time', 'spit_like_count')


class SpitCommentSerializer(serializers.ModelSerializer):
    """评论显示序列化器"""
    class Meta:
        model = Comments
        fields = ('comment','user_id')


class SpitDetailSerializer(serializers.ModelSerializer):
    """吐槽详情序列化器"""
    id = serializers.IntegerField(label='ID', read_only=True)
    comments_set = SpitCommentSerializer(many=True)

    class Meta:
        model = Spit
        fields = ('id','user_id', 'content', 'create_time', 'image', 'comments_set')


class SpitPublishSerializer(serializers.ModelSerializer):
    """发布吐槽序列化器"""
    user_id = serializers.IntegerField(label='用户')
    image = serializers.CharField(max_length=128,label='图片')
    create_time = serializers.DateTimeField(label='吐槽时间')
    content = serializers.CharField(min_length=1,label='吐槽详情')

    class Meta:
        model = Spit
        fields = ('user_id', 'content', 'create_time', 'image')

    def create(self, validated_data):
        return Spit.objects.create(**validated_data)







